﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web.Configuration;
using System.Web.Security;

namespace Tizer.Utilities
{
    public class PasswordHelper
    {
        private const string ConfigKey = "HashPasswords";
        private const string HashAlgorithm = "SHA1";
        private const int SaltLength = 16;

        public static string HashPassword(string password, string passwordSalt)
        {
            bool useHash;
            return (bool.TryParse(WebConfigurationManager.AppSettings.Get(ConfigKey), out useHash) && useHash)
                    ? FormsAuthentication.HashPasswordForStoringInConfigFile(passwordSalt + password, HashAlgorithm)
                    : password.PadRight(40);
        }

        public static string GeneratePasswordSalt()
        {
            var rng = new RNGCryptoServiceProvider();
            var salt = new byte[SaltLength];
            rng.GetBytes(salt);
            return Convert.ToBase64String(salt);
        }


    }
}
